@extends('admin.layouts.app')

@section('title', '用户管理')

@section('content')
<div class="container-fluid">
    <!-- 页面头部 -->
    <div class="d-flex justify-content-between align-items-center mb-4 mt-3">
        <div>
            <h3 class="fw-bold text-dark mb-1">用户管理</h3>
            <p class="text-muted mb-0">管理系统用户和权限分配</p>
        </div>
        @can('create-users')
        <a href="{{ route('admin.users.create') }}" class="btn btn-primary">
            <i class="fas fa-plus me-2"></i>添加用户
        </a>
        @endcan
    </div>

    <!-- 用户统计卡片 -->
    <div class="row g-4 mb-4">
        <div class="col-xxl-3 col-xl-4 col-lg-6 col-md-6">
            <div class="card stats-card h-100">
                <div class="card-header bg-primary text-white">
                    <div class="d-flex align-items-center justify-content-between">
                        <div>
                            <i class="fas fa-users fs-4 mb-2"></i>
                            <h6 class="mb-0 text-uppercase fw-bold letter-spacing">总用户数</h6>
                        </div>
                        <div class="text-end">
                            <h2 class="fw-bold mb-0">{{ $users->total() }}</h2>
                            <small class="opacity-75">系统用户</small>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center text-muted">
                        <i class="fas fa-chart-line text-success me-2"></i>
                        <small>活跃用户管理</small>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-xxl-3 col-xl-4 col-lg-6 col-md-6">
            <div class="card stats-card h-100">
                <div class="card-header bg-success text-white">
                    <div class="d-flex align-items-center justify-content-between">
                        <div>
                            <i class="fas fa-user-check fs-4 mb-2"></i>
                            <h6 class="mb-0 text-uppercase fw-bold letter-spacing">已验证</h6>
                        </div>
                        <div class="text-end">
                            <h2 class="fw-bold mb-0">{{ $users->where('email_verified_at', '!=', null)->count() }}</h2>
                            <small class="opacity-75">邮箱验证</small>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center text-muted">
                        <i class="fas fa-check-circle text-info me-2"></i>
                        <small>邮箱验证状态</small>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xxl-3 col-xl-4 col-lg-6 col-md-6">
            <div class="card stats-card h-100">
                <div class="card-header bg-info text-white">
                    <div class="d-flex align-items-center justify-content-between">
                        <div>
                            <i class="fas fa-user-tag fs-4 mb-2"></i>
                            <h6 class="mb-0 text-uppercase fw-bold letter-spacing">有角色</h6>
                        </div>
                        <div class="text-end">
                            <h2 class="fw-bold mb-0">{{ $users->filter(function($user) { return $user->roles->count() > 0; })->count() }}</h2>
                            <small class="opacity-75">分配角色</small>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center text-muted">
                        <i class="fas fa-shield-alt text-warning me-2"></i>
                        <small>权限管理</small>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xxl-3 col-xl-4 col-lg-6 col-md-6">
            <div class="card stats-card h-100">
                <div class="card-header bg-warning text-white">
                    <div class="d-flex align-items-center justify-content-between">
                        <div>
                            <i class="fas fa-building fs-4 mb-2"></i>
                            <h6 class="mb-0 text-uppercase fw-bold letter-spacing">组织用户</h6>
                        </div>
                        <div class="text-end">
                            <h2 class="fw-bold mb-0">{{ $users->filter(function($user) { return $user->organizations->count() > 0; })->count() }}</h2>
                            <small class="opacity-75">组织成员</small>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center text-muted">
                        <i class="fas fa-sitemap text-primary me-2"></i>
                        <small>组织架构</small>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 用户列表卡片 -->
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white border-0 py-3">
            <h5 class="fw-bold mb-0">
                <i class="fas fa-list text-primary me-2"></i>
                用户列表
            </h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="border-0 fw-bold text-dark">#</th>
                            <th class="border-0 fw-bold text-dark">用户信息</th>
                            <th class="border-0 fw-bold text-dark">角色权限</th>
                            <th class="border-0 fw-bold text-dark">组织架构</th>
                            <th class="border-0 fw-bold text-dark">创建时间</th>
                            <th class="border-0 fw-bold text-dark text-center">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($users as $user)
                        <tr class="border-bottom">
                            <td class="align-middle">
                                <div class="d-flex align-items-center">
                                    <div class="avatar bg-primary text-white rounded-circle d-flex align-items-center justify-content-center me-2" style="width: 32px; height: 32px; font-size: 12px;">
                                        {{ strtoupper(substr($user->name, 0, 1)) }}
                                    </div>
                                    <span class="fw-bold text-dark">#{{ $user->id }}</span>
                                </div>
                            </td>
                            <td class="align-middle">
                                <div>
                                    <div class="fw-bold text-dark mb-1">{{ $user->name }}</div>
                                    <div class="text-muted small">
                                        <i class="fas fa-envelope me-1"></i>{{ $user->email }}
                                    </div>
                                    @if($user->email_verified_at)
                                        <span class="badge bg-success-subtle text-success mt-1">
                                            <i class="fas fa-check-circle me-1"></i>已验证
                                        </span>
                                    @else
                                        <span class="badge bg-warning-subtle text-warning mt-1">
                                            <i class="fas fa-exclamation-circle me-1"></i>未验证
                                        </span>
                                    @endif
                                </div>
                            </td>
                            <td class="align-middle">
                                @if($user->roles->count() > 0)
                                    @foreach($user->roles as $role)
                                        <span class="badge bg-primary me-1 mb-1">
                                            <i class="fas fa-user-tag me-1"></i>{{ $role->name }}
                                        </span>
                                    @endforeach
                                @else
                                    <span class="text-muted small">
                                        <i class="fas fa-minus-circle me-1"></i>无角色
                                    </span>
                                @endif
                            </td>
                            <td class="align-middle">
                                @if($user->organizations->count() > 0)
                                    @foreach($user->organizations->take(2) as $organization)
                                        <span class="badge bg-info me-1 mb-1">
                                            <i class="fas fa-building me-1"></i>{{ $organization->name }}
                                        </span>
                                    @endforeach
                                    @if($user->organizations->count() > 2)
                                        <span class="text-muted small">+{{ $user->organizations->count() - 2 }}...</span>
                                    @endif
                                @else
                                    <span class="text-muted small">
                                        <i class="fas fa-minus-circle me-1"></i>无组织
                                    </span>
                                @endif
                            </td>
                            <td class="align-middle">
                                <div class="text-dark">{{ $user->created_at->format('Y-m-d') }}</div>
                                <div class="text-muted small">{{ $user->created_at->format('H:i') }}</div>
                            </td>
                            <td class="align-middle text-center">
                                <div class="btn-group" role="group">
                                    <a href="{{ route('admin.users.show', $user) }}" class="btn btn-sm btn-outline-info" title="查看详情">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    @can('edit-users')
                                    <a href="{{ route('admin.users.edit', $user) }}" class="btn btn-sm btn-outline-warning" title="编辑用户">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    @endcan
                                    @can('delete-users')
                                    <form action="{{ route('admin.users.destroy', $user) }}" method="POST" class="d-inline" 
                                          onsubmit="return confirm('确定要删除这个用户吗？')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-outline-danger" title="删除用户">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </form>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <div class="text-muted">
                                    <i class="fas fa-users fa-3x mb-3 opacity-50"></i>
                                    <div class="h5">暂无用户数据</div>
                                    <p class="mb-0">系统中还没有用户，请先添加用户。</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            
            @if($users->hasPages())
            <div class="mt-4">
                <div class="pagination-info">
                    显示第 {{ $users->firstItem() }} - {{ $users->lastItem() }} 条，共 {{ $users->total() }} 条记录
                </div>
                <div class="d-flex justify-content-center">
                    {{ $users->links() }}
                </div>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>
.page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 0.5rem;
    margin-bottom: 2rem;
}

.stats-card {
    transition: all 0.3s ease;
    border: none;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.stats-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}

.stats-icon {
    width: 48px;
    height: 48px;
    border-radius: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
}

.table {
    border-radius: 0.5rem;
    overflow: hidden;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.table th {
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    border-bottom: 2px solid #dee2e6;
    font-weight: 600;
    color: #495057;
    padding: 1rem 0.75rem;
    position: relative;
    border-top: none;
}

.table tbody tr {
    transition: all 0.2s ease;
}

.table-hover tbody tr:hover {
    background-color: #f8f9fa;
    transform: scale(1.01);
}

.avatar {
    transition: all 0.3s ease;
}

.avatar:hover {
    transform: scale(1.1);
}

.btn-group .btn {
    margin-right: 2px;
    transition: all 0.2s ease;
}

.btn-group .btn:hover {
    transform: translateY(-1px);
}

.btn-group .btn:last-child {
    margin-right: 0;
}

.letter-spacing {
    letter-spacing: 0.05em;
}

.stats-card .card-header {
    position: relative;
    overflow: hidden;
}

.stats-card .card-header::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(45deg, transparent 30%, rgba(255,255,255,0.1) 50%, transparent 70%);
    transform: translateX(-100%);
    transition: transform 0.6s ease;
}

.stats-card:hover .card-header::before {
    transform: translateX(100%);
}
</style>
@endpush